import $api from "../api.js"
//字典取数据功能
//options设置集
// {
// 	key: 关键字
// 	value: 匹配值
//	resKey: 返回指定键名的数据
//	match: 匹配模式，includes指定字典关键字的值中包含匹配值；beIncludes匹配值包含指定字典关键字的值
// }
//options为字符串时，表示去字典中 key 等于 options值的字典对象
export default function getEnum(dataList, options){
	let key, value, resKey, match;
	if($api.isString(options)){
		key = "key";
		value = options;
	}else if($api.isObject(options)){
		({key="key", value, resKey, match} = options);
	}
	if(value!==null && value!=="" && value!==undefined){
		let res;
		//匹配类型
		switch(match){
			//包含
			case "includes":
				res = $api.arrFind(dataList, {format:"item",
					func:function(item){
						if(item[key].includes(value)){
							return true
						}
						return false
					},
				});
				break;
			//被包含
			case "beIncludes":
				res = $api.arrFind(dataList, {format:"item",
					func:function(item){
						if(value.includes(item[key])){
							return true
						}
						return false
					},
				});
				break;
			//默认值相等
			default:
				res = $api.arrFind(dataList, {key:key, value:value, format:"item"});
		}
		return resKey&&res? res[resKey] :res
	}
	return $api.arrToObj(dataList, {key:"key", value:function(item){return item}})
}